package org.leetcodeplus.leetcodeplus.api.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import org.leetcodeplus.leetcodeplus.api.entity.LcTest;
import org.leetcodeplus.leetcodeplus.api.entity.LcTestVo;
import org.leetcodeplus.leetcodeplus.api.entity.Result;
import org.leetcodeplus.leetcodeplus.api.service.ILcTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-31
 */
@RestController
@RequestMapping("/lcTest")
public class LcTestController {

    @Autowired
    private IService<LcTest> ITestService;

    @Autowired
    private ILcTestService lcTestService;

    /*
    TODO：批量新增题目数据
     */
    @PostMapping("/insertTests")
    public Result insertTests(@RequestBody Collection<LcTest> lcTests) {
        // 检查数据是否已经存在
        List<LcTest> existingTests = lcTestService.findExistingTests(lcTests);
        // 如果存在重复数据，返回失败
        if (!existingTests.isEmpty()) {
            return Result.fail("批量插入失败，存在重复数据");
        }
        boolean success = ITestService.saveBatch(lcTests);
        return success ? Result.ok("批量插入成功") : Result.fail("批量插入失败");
    }

    /*
    TODO：修改题库状态
     */
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody LcTest lcTest) {
        boolean success = lcTestService.updateById(lcTest);
        return success ? Result.ok("修改成功") : Result.fail("修改失败");
    }

    /*
    TODO：根据分类查询题库
     */
    @GetMapping("/getTests")
    public List<LcTest> getTests(@RequestParam(required = false) String parentId,@RequestParam(required = false) String status){
        return lcTestService.getTests(parentId,status);
    }

    /*
    TODO：查看单条题库状态
     */
    @GetMapping("/getLcByCode")
    public LcTestVo getLcByCode(@RequestParam String code){
        return lcTestService.getLcByCode(code);
    }


}
